package com.boarsoft.wechat.action;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.boarsoft.bean.ReplyInfo;
import com.boarsoft.boar.common.Constants;
import com.boarsoft.common.Util;
import com.boarsoft.common.dao.PagedResult;
import com.boarsoft.common.util.JsonUtil;
import com.boarsoft.wechat.biz.WeChatBiz;
import com.boarsoft.wechat.entity.WeChatTemplate;
import com.boarsoft.wechat.service.TempletService;

/**
 * 微信内容模板维护Action
 * 
 * @author xuhui.guo
 *
 */
@RestController
@RequestMapping("/wechat")
public class WeChatAction {
	private static final Logger log = LoggerFactory.getLogger(WeChatAction.class);

	@Autowired
	private WeChatBiz weChatBiz;
	@Autowired
	private TempletService templateService;

	@RequestMapping("/list")
	public ReplyInfo<Object> list(String orderBy, int pageNo, int pageSize) {
		try {
			PagedResult<WeChatTemplate> templates = weChatBiz.list(orderBy, pageNo, pageSize);
			return new ReplyInfo<Object>(true, JsonUtil.from(templates, new String[] { "msgTypes" }));
		} catch (Exception e) {
			log.error("Error to query wechat template info.", e);
			return new ReplyInfo<Object>(Constants.ERROR_UNKNOWN, new String[] { "查询微信内容模板" });
		}
	}

	@RequestMapping("/add")
	public ReplyInfo<Object> add(WeChatTemplate template) {
		template.setStatus(WeChatTemplate.WECHAT_TEMPLATE_STATUS_UNPUBLISH);
		if (!template.validate()) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, new String[] { "参数" });
		}
		try {
			weChatBiz.save(template);
			return ReplyInfo.SUCCESS;
		} catch (Exception e) {
			log.error("Error to save wechat template info.", e);
			return new ReplyInfo<Object>(Constants.ERROR_UNKNOWN, new String[] { "保存微信内容模板" });
		}
	}

	@RequestMapping("/edit")
	public ReplyInfo<Object> edit(WeChatTemplate template) {
		if (!template.validate()) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, new String[] { "参数" });
		}
		try {
			weChatBiz.save(template);

			if (WeChatTemplate.WECHAT_TEMPLATE_STATUS_PUBLISH.equals(template.getStatus())) {
				templateService.upadteTemplet("2", template);
			}

			return ReplyInfo.SUCCESS;
		} catch (Exception e) {
			log.error("Error to update wechat template info.", e);
			return new ReplyInfo<Object>(Constants.ERROR_UNKNOWN, new String[] { "更新微信内容模板" });
		}
	}

	@RequestMapping("/delete")
	public ReplyInfo<Object> delete(String id, String status) {
		if (Util.strIsEmpty(id)) {
			return new ReplyInfo<Object>(Constants.WARN_INVALID, new String[] { "参数" });
		}
		try {
			WeChatTemplate template = weChatBiz.get(id);
			// 有消息绑定模板
			if (template.getMsgTypes() != null && template.getMsgTypes().size() > 0) {
				return new ReplyInfo<Object>(Constants.WARN_INVALID, new String[] { template.getCode() });
			} else {
				weChatBiz.delete(id);
				if (WeChatTemplate.WECHAT_TEMPLATE_STATUS_PUBLISH.equals(status)) {
					templateService.upadteTemplet("3", template);
				}
				return ReplyInfo.SUCCESS;
			}
		} catch (Exception e) {
			log.error("Error to delete wechat template info.", e);
			return new ReplyInfo<Object>(Constants.ERROR_UNKNOWN, new String[] { "删除微信内容模板" });
		}
	}

	@RequestMapping("/publish")
	public ReplyInfo<Object> publish(String id) {
		try {
			WeChatTemplate template = weChatBiz.get(id);
			template.setStatus(WeChatTemplate.WECHAT_TEMPLATE_STATUS_PUBLISH);
			weChatBiz.save(template);
			templateService.upadteTemplet("1", template);
			return ReplyInfo.SUCCESS;
		} catch (Exception e) {
			log.error("Error to publish wechat template info.", e);
			return new ReplyInfo<Object>(Constants.ERROR_UNKNOWN, new String[] { "发布微信内容模板" });
		}
	}

	@RequestMapping("/uninstall")
	public ReplyInfo<Object> uninstall(String id) {
		try {
			WeChatTemplate template = weChatBiz.get(id);
			template.setStatus(WeChatTemplate.WECHAT_TEMPLATE_STATUS_UNPUBLISH);
			weChatBiz.save(template);
			templateService.upadteTemplet("3", template);
			return ReplyInfo.SUCCESS;
		} catch (Exception e) {
			log.error("Error to uninstall wechat template info.", e);
			return new ReplyInfo<Object>(Constants.ERROR_UNKNOWN, new String[] { "卸载微信内容模板" });
		}
	}
}